<?php

abstract class Light_Module
{
    protected $_name;

    protected $_config = array();

    public function events()
    {

    }

    public function services()
    {

    }

    /**
     * @param string | array $event
     * @param callback $callback
     */
    protected function _attachEvent($event, $callback)
    {
        return Light_Event_Manager::getInstance()->attach($event, $callback);
    }

    /**
     * @param string $name
     * @param mixed $service
     * @param mixed $params
     * @throws Light_Exception
     * @return Light_Module
     */
    protected function _setService($name, $service, array $params = array())
    {
        Light_Service_Manager::getInstance()->set($name, $service, $params);

        return $this;
    }

    /**
     * Load configs from each module
     */
    final public function getConfigs()
    {
        $module = $this->getName();

        $configFiles = glob(LIGHT_APPLICATION_PATH . '/modules/' . $module . '/configs/{global,local}.php', GLOB_BRACE);

        if (is_array($configFiles) && !empty($configFiles)) {
            foreach ($configFiles as $configFile) {
                $this->_config = Light_Util::merge($this->_config, require $configFile);
            }
        }

        foreach ($this->_config as $section => $params) {
            switch ($section) {
                case 'routes' :
                    Light_Core::addRoutes($params);
                    break;
            }
        }
    }

	/**
     * @return string $_name
     */
    final public function getName ()
    {
        if ($this->_name === null) {
            $module = get_class($this);
            $module = explode('_', $module);
            $this->_name = strtolower($module[0]);
        }

        return $this->_name;
    }
}